﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DALBase;
using OneMile.Common;
using OneMile.Entity;
using OneMile.Entity.Model;
using System.Collections;
using System.Data;

namespace OneMile.DAL
{
    public class DALUUserAddress:BaseDAL
    {
        public DBAccess db = DBConnection.CreateDB();

        public bool DelUserAddress(string userAddressId)
        {
            try
            {
               StringBuilder sb = new StringBuilder();
               sb.AppendLine("UPDATE Fct_UserAddress ");
               sb.AppendLine("SET Disabled=1 ");
               sb.AppendLine("WHERE UserAddressId = @UserAddressId");
               Hashtable hs = new Hashtable();
               hs.Add("UserAddressId", userAddressId);
               int num = Convert.ToInt32(db.ExecuteNonQuery(sb.ToString(),hs));
               if (num == 1)
               {
                   return true;
               }
               else
               {
                   return false;
               }
            }
            catch (Exception e)
            {
               base.WriteLog(e.Message);
               throw;
            }

        }

        public bool UpdateUserAddress(EUserAddress userAddress)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("UPDATE Fct_UserAddress ");
                sb.AppendLine("    SET AddressName=@AddressName,UserId=@UserId,Address=@Address,Consignee=@Consignee,Mobile=@Mobile,Rec_ModifyTime=@Rec_ModifyTime,Rec_ModifyBy=@Rec_ModifyBy");
                sb.AppendLine("    WHERE UserAddressId=@UserAddressId");

                Hashtable hs = new Hashtable();
                hs.Add("UserAddressId", userAddress.UserAddressId);
                hs.Add("UserId", userAddress.UserId);
                hs.Add("AddressName", userAddress.AddressName);
                hs.Add("Address", userAddress.Address);
                hs.Add("Consignee", userAddress.Consignee);
                hs.Add("Mobile", userAddress.Mobile);
                //hs.Add("Disabled", userAddress.Disabled);
                //hs.Add("Rec_CreateTime", userAddress.Rec_CreateTime);
                //hs.Add("Rec_CreateBy", userAddress.Rec_CreateBy);
                hs.Add("Rec_ModifyTime", DateTime.Now);
                hs.Add("Rec_ModifyBy", userAddress.Rec_ModifyBy);

                int num = Convert.ToInt32(db.ExecuteNonQuery(sb.ToString(), hs));
                if (num == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public bool IsAddressExit(string AddressName, string UserId)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("SELECT COUNT(1) FROM Fct_UserAddress WHERE Disabled=0  AND UserId=@UserId AND AddressName=@AddressName");
                Hashtable hs = new Hashtable();
                hs.Add("AddressName", AddressName);
                hs.Add("UserId", UserId);
                int num = Convert.ToInt32(db.ExecuteScalar(sb.ToString(), hs));
                if (num == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public bool AddUserAddress(EUserAddress userAddress)
        {
            try
            {
               StringBuilder sb = new StringBuilder();
               sb.AppendLine("INSERT INTO Fct_UserAddress ");
               sb.AppendLine("(UserAddressId,AddressName,UserId,Address,Consignee,Mobile,Rec_CreateTime,Rec_CreateBy,Rec_ModifyTime,Rec_ModifyBy,Disabled)");
               sb.AppendLine("    VALUES");
               sb.AppendLine("         (");             
               sb.AppendLine("           @UserAddressId,");
               sb.AppendLine("           @AddressName,");
               sb.AppendLine("           @UserId,");
               sb.AppendLine("           @Address,");
               sb.AppendLine("           @Consignee,");
               sb.AppendLine("           @Mobile,");
               sb.AppendLine("           @Rec_CreateTime,");
               sb.AppendLine("           @Rec_CreateBy,");
               sb.AppendLine("           @Rec_ModifyTime,");
               sb.AppendLine("           @Rec_ModifyBy,");
               sb.AppendLine("           @Disabled");
               sb.AppendLine("         )");

               Hashtable hs = new Hashtable();
               hs.Add("UserAddressId", userAddress.UserAddressId);          
               hs.Add("AddressName", userAddress.AddressName);
               hs.Add("UserId", userAddress.UserId);
               hs.Add("Address", userAddress.Address);
               hs.Add("Consignee", userAddress.Consignee);
               hs.Add("Mobile", userAddress.Mobile);
               hs.Add("Rec_CreateTime", DateTime.Now);
               hs.Add("Rec_CreateBy", userAddress.Rec_CreateBy);
               hs.Add("Rec_ModifyTime", DateTime.Now);
               hs.Add("Rec_ModifyBy", userAddress.Rec_ModifyBy);
               hs.Add("Disabled", userAddress.Disabled);

               int num = Convert.ToInt32(db.ExecuteNonQuery(sb.ToString(), hs));
               if (num == 1)
               {
                   return true;
               }
               else
               {
                   return false;
               }
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }                    
        }

        public IList<EUserAddress> GetUserAddressListByUserId(string userId)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" SELECT UserAddressId,AddressName,UserId,Address,Consignee,Mobile");
                sb.AppendLine("   FROM Fct_UserAddress ");
                sb.AppendLine("   WHERE UserId=@UserId and Disabled=0");

                Hashtable hs = new Hashtable();
                hs.Add("UserId", userId);

                DataTable dt = db.ExecuteQuery(sb.ToString(), hs);
                IList<EUserAddress> list = null;
                if (dt.Rows.Count > 0)
                {
                    list = new List<EUserAddress>();
                    EUserAddress userAddress = null;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        userAddress = new EUserAddress();
                        Guid UserAddressId = new Guid(dt.Rows[i]["UserAddressId"].TString());
                        Guid UserId = new Guid(dt.Rows[i]["UserId"].TString());
                        userAddress.UserAddressId = UserAddressId;
                        userAddress.UserId = UserId;
                        userAddress.AddressName = dt.Rows[i]["AddressName"].TString();
                        userAddress.Address = dt.Rows[i]["Address"].TString();
                        userAddress.Consignee = dt.Rows[i]["Consignee"].TString();
                        userAddress.Mobile = dt.Rows[i]["Mobile"].TString();
                        list.Add(userAddress);
                    }
                }
                return list;
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                return null;
            }
        }
    }
}
